//
//  KSOrderReviewViewController.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/25.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

/// 审核订单
class KSOrderReviewViewController: KSBaseViewController {

    @IBOutlet weak var tableView: UITableView!
    var orderId = ""
    private var model: KSGetOrderDetailModel?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = "审核订单"
        
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.separatorColor = KSColor.kColor_line
        self.tableView.backgroundColor = KSColor.kColorViewBackground
        
        let orderStatusNib = UINib(nibName: "KSOrderStatusTableViewCell", bundle: nil)
        self.tableView.register(orderStatusNib, forCellReuseIdentifier: "KSOrderStatusTableViewCell")
        
        let addressNib = UINib(nibName: "KSAddressTableViewCell", bundle: nil)
        self.tableView.register(addressNib, forCellReuseIdentifier: "KSAddressTableViewCell")
        
        let messageNib = UINib(nibName: "KSMessageTableViewCell", bundle: nil)
        self.tableView.register(messageNib, forCellReuseIdentifier: "KSMessageTableViewCell")
        
        let orderNoNib = UINib(nibName: "KSOrderNoTableViewCell", bundle: nil)
        self.tableView.register(orderNoNib, forCellReuseIdentifier: "KSOrderNoTableViewCell")
        
        self.startLoading()
        self.requestData()
    }

    private func requestData() {
        self.request.requestGetOrderDetail(orderId: orderId)
    }
    
    override func requestSuccess(result: String, apiType: KSApiType) {
        super.requestSuccess(result: result, apiType: apiType)
        model = KSGetOrderDetailModel.deserialize(from: result)
        tableView.reloadData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // 审核驳回
    @IBAction func rebutBtnClick(_ sender: Any) {
        let vc = KSReviewRebutViewController()
        vc.orderId = orderId
        self.navigationController?.pushViewController(vc, animated: true)
    }
    // 审核通过
    @IBAction func passBtnClick(_ sender: Any) {
        let vc = KSReviewPassViewController()
        vc.orderId = orderId
        self.navigationController?.pushViewController(vc, animated: true)
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension KSOrderReviewViewController: UITableViewDelegate, UITableViewDataSource {
    // UITableViewDataSource
    
    func numberOfSections(in tableView: UITableView) -> Int {
        var num = 0
        if model?.data != nil && (model?.data?.order_goods?.count)! > 0 {
            num = 2 + (model?.data?.order_goods?.count)!
        }
        return num
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var num = (model?.data?.order_goods?.count)!
        if section == 0 || section == (model?.data?.order_goods?.count)! + 3 - 2 {
            num = 3
        } else if section == (model?.data?.order_goods?.count)! + 3 - 1 {
            num = 1
        }
        
        return num
    }
    
    // UITableViewDelegate
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        var height = 0.0
        if indexPath.section == 0 {
            if indexPath.row == 2 {
                height = (model?.data?.mark.isEmpty)! ? 0 : 90
            } else {
                height = 90.0
            }
        } else if indexPath.section == (model?.data?.order_goods?.count)! + 3 - 2 || indexPath.section == (model?.data?.order_goods?.count)! + 3 - 1 {
            height = 44.0
        } else {
            height = 110.0 + 65
        }
        
        return CGFloat(height)
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "KSOrderStatusTableViewCell", for: indexPath) as! KSOrderStatusTableViewCell
                cell.updateCellData(reviewStatu: "买家已付款，请尽快审核！", reviewPeople: "2天11小时后订单自动关闭", statusImage: "Home_ReviewIn")
                return cell
            } else if indexPath.row == 1 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "KSAddressTableViewCell", for: indexPath) as! KSAddressTableViewCell
                cell.nameLabel.text = model?.data?.name
                cell.addressLabel.text = model?.data?.user_area_info
                return cell
            } else if indexPath.row == 2 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "KSMessageTableViewCell", for: indexPath) as! KSMessageTableViewCell
                cell.isHidden = (model?.data?.mark.isEmpty)!
                return cell
            }
        } else if indexPath.section == (model?.data?.order_goods?.count)! + 2 - 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "KSOrderNoTableViewCell", for: indexPath) as! KSOrderNoTableViewCell
            cell.titleLabel.isHidden = false
            cell.accessoryType = .none
            if indexPath.row == 0 {
                cell.titleLabel.text = "运费"
                cell.valueLabel.text = "\(Int((model?.data?.shipping_fee)!))"
                cell.valueLabel.snp.updateConstraints({ (make) in
                    make.right.equalTo(-10)
                })
                cell.accessoryType = .disclosureIndicator
            }
            else if indexPath.row == 1 {
                cell.titleLabel.text = "订单号"
                cell.valueLabel.text = model?.data?.order_number
            } else {
                cell.titleLabel.text = "下单时间"
                cell.valueLabel.text = model?.data?.add_time
            }

            return cell
        }
        
        let cell = KSOrderSpecTableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "KSOrderSpecTableViewCell" + String.init(indexPath.section))
        cell.setDataWith(model: (model?.data?.order_goods)![indexPath.section - 1])
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 3 && indexPath.row == 0 {
            self.navigationController?.pushViewController(KSExpressExplainViewController(), animated: true)
        }
    }
}
